﻿<template>
  <div class="app-container">
    <div class="chart-title">档案管理</div>
    <div class="chart-content">
      <div class="filter-title">
        <el-form label-position="right">
          <el-form-item>
            <el-select
              v-model="listQuery.region_type"
              style="width: 160px; height:32px;"
              clearable
              placeholder="请选择区域类型"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="listQuery.region_name"
              style="width: 160px; height:32px;"
              class="filter-item"
              :placeholder="$t('region.name')"
              @keyup.enter.native="handleFilter"
            ></el-input>
          </el-form-item>
          <div
            class="search-button"
            @click="handleFilter"
          >
            <img src="@/assets/images/bg_search@2x.png" />
          </div>
          <div
            class="search-button"
            @click="handleRefresh"
          >
            <img src="@/assets/images/bg_reset@2x.png" />
          </div>
        </el-form>
      </div>
      <div class="chart-list">
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(255,255,255,0.2)"
          border
          fit
          highlight-current-row
          style="width: 100%min-height:1000px"
        >
          <el-table-column
            fixed="left"
            align="center"
            :label="$t('table.id')"
            width="65"
          >
            <template slot-scope="scope">
              <span>{{
                (listQuery.page - 1) * listQuery.limit + scope.$index + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('region.id')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('region.countryName')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.country_name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('region.provinceName')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.province_name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('region.districtName')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.district_name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('region.name')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.region_name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('region.type')"
          >
            <template slot-scope="scope">
              <span>{{ regionTypeEs(scope.row.region_type) }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
import { getToken } from "@/utils/auth";
export default {
  name: "sevenDistricts",
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      temp: {
        name: "",
        linkman: "",
        linkman_mobile: ""
      },
      listQuery: {
        page: 1,
        limit: 20,
        region_type: undefined,
        region_name: ""
      },
      typeOptions: [{
        value: 0,
        label: '国家'
      }, {
        value: 1,
        label: '省份'
      }, {
        value: 2,
        label: '城市'
      }, {
        value: 3,
        label: '区/县'
      }],
      districtsOptions: [],
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        read: "查看"
      }
    };
  },
  created() {
    const token = getToken()
    if (token && !this.$store.state.socket.loginTransition && !this.$store.state.socket.loginAccount) {
      this.tokenLogin(token)
    }
    this.getList();
  },
  methods: {
    tokenLogin(token) {
      const path = {
        app: "wxadmin",
        api: "api_wxadmin_admin_keepLogin",
        version: "1.0",
        type: 2,
        data: {
          token: token
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        this.socketData(res);
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        region_type: undefined,
        region_name: ""
      };
      this.getList();
    },
    // 获取区域列表
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page,
        region_type: this.listQuery.region_type || this.listQuery.region_type === 0 ? this.listQuery.region_type : "",
        region_name: this.listQuery.region_name ? this.listQuery.region_name : ""
      };
      const itocIndexPath = {
        company: "STQ",
        api: "api_archives_region_getList",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(itocIndexPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "api_wxadmin_admin_keepLogin") {
            this.$store.dispatch("loginTransition", true);
            this.socketApi.sendCache(res => {
              this.socketData(res);
            })
          } else if (resj.api === "api_archives_region_getList") {
            this.list = resj.data;
            this.total = resj.count;
            this.districtsOptions = resj.data;
            this.listLoading = false;
          }
        }
      }
    }
  }
};
</script>
